/**
 * 
 */
package com.taobao.client.buyer;

import java.util.HashMap;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.taobao.client.Data;
import com.taobao.client.RPC;
import com.taobao.client.buyer.custom.CustomButton;
import com.taobao.client.obj.TopOrder;
import com.taobao.client.obj.TopRefund;
import com.taobao.client.obj.TopTrade;
import com.taobao.client.pages.AddTradeMemo;
import com.taobao.client.pages.BuyerReturnGoods;
import com.taobao.client.pages.GoodsConfirmation;
import com.taobao.client.pages.RefundApply;
import com.taobao.client.pages.RefundTermUpdate;
import com.taobao.client.util.PopBox;

/**
 * A vertical panel that displays a list of trades
 * 
 * @author hujingfei.pt@taobao.com
 * 
 */
public class BoughtItemList extends VerticalPanel implements ClickHandler {

	private String addTradeMemo = "添加订单描述";

	private static final int VISIBLE_ITEM_COUNT = 5;

	private HTML countLabel = new HTML();
	private HTML newerButton = new HTML(
			"<a href='javascript:;'>&lt; newer</a>", true);
	private HTML olderButton = new HTML(
			"<a href='javascript:;'>older &gt;</a>", true);
	private FlexTable flexTable = new FlexTable();
	private HorizontalPanel navBar = new HorizontalPanel();
	private VerticalPanel itemInfoVP = new VerticalPanel();

	private int startIndex;

	private List<TopTrade> bil;
	// A middle variable to store the id of the trade
	private String url;
	// A list of top refunds
	private List<TopRefund> trList;
	private RefundApply refundApplyPage;
	// The refund term update page.
	private RefundTermUpdate rTUPage;
	// The buyer return goods page.
	private BuyerReturnGoods bRGPage;
	// The buyer-confirm-goods page
	private GoodsConfirmation gCPage;

	/**
	 * Default constructor.
	 */
	public BoughtItemList() {
		super();
		newerButton.addClickHandler(this);
		olderButton.addClickHandler(this);

		flexTable.addClickHandler(this);
	}

	public void initialize(List<TopTrade> bil, Data data) {

		this.bil = bil;
		this.add(flexTable);

		flexTable.setBorderWidth(2);
		flexTable.setCellPadding(5);
		flexTable.setCellSpacing(5);

		this.add(itemInfoVP);

		// Create the 'navigation' bar at the upper-right.
		HorizontalPanel innerNavBar = new HorizontalPanel();

		innerNavBar.add(newerButton);
		innerNavBar.add(countLabel);
		innerNavBar.add(olderButton);

		navBar.setHorizontalAlignment(HorizontalPanel.ALIGN_CENTER);
		navBar.add(innerNavBar);

		initTable();
		update();
	}

	/**
	 * Setter and getter methods
	 * 
	 */
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public FlexTable getFlexTable() {
		return flexTable;
	}

	public void setFlexTable(FlexTable flexTable) {
		this.flexTable = flexTable;
	}

	public List<TopRefund> getTrList() {
		return trList;
	}

	public void setTrList(List<TopRefund> trList) {
		this.trList = trList;
	}

	@Override
	public void onClick(ClickEvent arg0) {
		Object sender = arg0.getSource();
		if (sender == olderButton) {
			// Move forward a page.
			startIndex += VISIBLE_ITEM_COUNT;
			if (startIndex >= bil.size()) {
				startIndex -= VISIBLE_ITEM_COUNT;
			} else {
				update();
			}
		} else if (sender == newerButton) {
			// Move back a page.
			startIndex -= VISIBLE_ITEM_COUNT;
			if (startIndex < 0) {
				startIndex = 0;
			} else {
				update();
			}
		} else if (sender instanceof Button) {
			Button tempB = (Button) sender;
			// Users click "Refund" button

			if (tempB.getText().equalsIgnoreCase(addTradeMemo)) {
				AddTradeMemo memo = new AddTradeMemo((CustomButton) tempB);
				memo.show();
			} else if (tempB.getText().equalsIgnoreCase("申请退款")) {

				final TopTrade trade = ((CustomButton) tempB).getTrade();
				TopOrder order = ((CustomButton) tempB).getOrder();

				final String orderId;
				if (order != null) {
					orderId = order.getTid();
				} else {
					orderId = trade.getTid();
				}

				// Get the refund apply page.
				refundApplyPage = RefundApply.getRefundApply();

				try {

					RPC.getService().getTdFullInfo(trade.getTid(), orderId, new AsyncCallback<TopTrade>() {
						
						@Override
						public void onSuccess(TopTrade topTrade) {
							// Set the children order remark.
							refundApplyPage.setcTradeRemark(topTrade.getBuyerMemo());
							refundApplyPage.setTradeId(orderId);
							// Set the parent order remark.
							refundApplyPage.setpTradeRemark(trade.getBuyerMemo());
							refundApplyPage.simulate();
						}
						
						@Override
						public void onFailure(Throwable caught) {
							PopBox.error("得到退款详情失败", caught.getLocalizedMessage());
						}
					});
				} catch (Exception e) {
					e.printStackTrace();
				}

				// refundApplyPage.
				// Get the trade.
				// TopTrade trade = ((CustomButton) tempB).getTrade();
				// TopOrder trade = ((CustomButton) tempB).getOrder();
				// Set the order id.

				// refundApplyPage.setcTradeRemark(order.get)
				// Set the
				// Add a new history token.
				// History.newItem(RefundApply.HISTORY_ITEM);
			} else if (tempB.getText().equalsIgnoreCase("修改退款协议")) {

				TopTrade trade = ((CustomButton) tempB).getTrade();
				TopOrder order = ((CustomButton) tempB).getOrder();

				String orderId = "";
				if (trade == null && order != null) {
					orderId = order.getTid();
				} else {
					orderId = trade.getTid();
				}

				// Get the refund term update page.
				rTUPage = RefundTermUpdate.getRefundTermUpdate();
				// Get the trade.
				// TopTrade trade = ((CustomButton) tempB).getTrade();
				// TopOrder trade = ((CustomButton) tempB).getOrder();
				// Set the trade id and the refund id.
				rTUPage.setIds(orderId, ((CustomButton) tempB).getRefundId());
				// Add a new history token.
				History.newItem(RefundTermUpdate.HISTORY_ITEM);
			} else if (tempB.getText().equalsIgnoreCase(confirmProduct)) {
				// Get the buyer-confirm-goods page.
				gCPage = GoodsConfirmation.getGoodConfirmation();

				TopTrade trade = ((CustomButton) tempB).getTrade();
				TopOrder order = ((CustomButton) tempB).getOrder();

				String orderId = "";
				if (trade == null && order != null) {
					orderId = order.getTid();
				} else {
					orderId = trade.getTid();
				}

				// Set the trade id.
				gCPage.setTradeId(orderId);
				// Add a new history token.
				History.newItem(GoodsConfirmation.HISTORY_ITEM);
			} else if (tempB.getText().equalsIgnoreCase("付款")) {
				HashMap<String, String> m = new HashMap<String, String>();

				TopTrade trade = ((CustomButton) tempB).getTrade();
				TopOrder order = ((CustomButton) tempB).getOrder();

				String orderId = "";
				if (trade == null && order != null) {
					orderId = order.getTid();
				} else {
					orderId = trade.getTid();
				}

				m.put("biz_order_id", orderId);
				m.put("biz_type", "620");
				m.put("encoding", "UTF-8");
				m.put("method", Data.PAY);

				RPC.getService().getUrl(m, new AsyncCallback<String>() {
					@Override
					public void onSuccess(String arg0) {
						System.out.println("Pay:" + arg0);
						// Window.open(arg0, null, null);
						PopBox.openWindow(arg0);
					}

					@Override
					public void onFailure(Throwable arg0) {
						PopBox.error("error with get url", arg0.getMessage());
					}
				});
			} else if (tempB.getText().equalsIgnoreCase("退货")) {

				TopTrade trade = ((CustomButton) tempB).getTrade();
				TopOrder order = ((CustomButton) tempB).getOrder();

				String orderId = "";
				if (trade == null && order != null) {
					orderId = order.getTid();
				} else {
					orderId = trade.getTid();
				}

				// Get the buyer return goods page
				bRGPage = BuyerReturnGoods.getBuyerReturnGoods();
				// Initialize input parameters
				bRGPage.setIds(orderId, ((CustomButton) tempB).getRefundId());
				// Add a new history token
				History.newItem(BuyerReturnGoods.HISTORY_ITEM);
			} else if (tempB.getText().equalsIgnoreCase("查看详情")) {

				final TopTrade tt = ((CustomButton) tempB).getTrade();

				final TopRefund refund = getRefund2(tt.getTid());

				// Display the concrete information about one item.
				final FlexTable ft = new FlexTable();
				ft.setBorderWidth(2);

				ft.setCellSpacing(5);
				ft.setCellPadding(5);

				int i = 0;
				ft.setText(i, 0, "商品标题");
				ft.setText(i, 1, tt.getTitle());
				ft.setText(++i, 0, "价格");
				ft.setText(i, 1, tt.getPrice());
				ft.setText(++i, 0, "交易状态");
				ft.setText(i, 1, StatusMap.get(tt.getStatus()));

				List<TopOrder> l = tt.getTopOrders();
				int size = 0;
				if (l != null)
					size = l.size();

				if (refund != null && size < 2) {// 有子订单则不显示退款相关资料
					// Invoke the TOP client API
					RPC.getService().getRefundInfo(refund.getRefundId(),
							new AsyncCallback<TopRefund>() {

								@Override
								public void onSuccess(TopRefund arg0) {

									int i = 2;
									ft.setText(++i, 0, "退款信息");
									ft.getFlexCellFormatter().setColSpan(i, 0,
											2);
									ft.getFlexCellFormatter()
											.setHorizontalAlignment(i, 0,
													ALIGN_CENTER);

									ft.setText(++i, 0, "退款状态:");
									ft.setText(i, 1, StatusMap.get(arg0
											.getStatus()));

									ft.setText(++i, 0, "退款人:");
									ft.setText(i, 1, arg0.getBuyerNick());
									ft.setText(++i, 0, "退款地址:");
									ft.setText(i, 1, arg0.getAddress());
									// good_return_time
									ft.setText(++i, 0, "退款时间:");
									ft
											.setText(
													i,
													1,
													(arg0.getGoodReturnTime()) == null ? "null"
															: arg0
																	.getGoodReturnTime()
																	.toString());

									ft.setText(++i, 0, "货物状态:");
									ft.setText(i, 1, StatusMap.get(arg0
											.getGoodStatus()));

									// ft.setText(++i, 0, "交易状态:");
									// ft.setText(i, 1, arg0.getOrderStatus());

									// ft.setText(++i, 0, text)

									// if (
									// (tt.getStatus().equalsIgnoreCase("WAIT_BUYER_CONFIRM_GOODS")
									// ||
									// tt.getStatus().equalsIgnoreCase("WAIT_SELLER_SEND_GOODS")
									// )
									// &&
									// (
									// (!arg0.getGoodStatus().equalsIgnoreCase("BUYER_RETURNED_GOODS")
									// &&
									// (
									// arg0.getStatus().equalsIgnoreCase("WAIT_SELLER_AGREE")
									// ||
									// arg0.getStatus().equalsIgnoreCase("SELLER_REFUSE_BUYER")
									// ))
									// ||
									// (arg0.getGoodStatus().equalsIgnoreCase("BUYER_RETURNED_GOODS")
									// && (
									// arg0.getStatus().equalsIgnoreCase("SELLER_REFUSE_BUYER")))
									//											
									// )
									// ) {
									Button updateRefTerB = new CustomButton(
											"修改退款协议");
									updateRefTerB
											.addClickHandler(BoughtItemList.this);

									// Set the trade, the trade id and the
									// refund id
									((CustomButton) updateRefTerB).setTrade(tt);
									((CustomButton) updateRefTerB)
											.setTradeId(tt.getTid());
									((CustomButton) updateRefTerB)
											.setRefundId(arg0.getRefundId());
									// Add to the alternative actions
									// itemInfoVP.add(updateRefTerB);
									ft.setWidget(++i, 0, updateRefTerB);
									// }
								}

								@Override
								public void onFailure(Throwable arg0) {
									PopBox.error("获取退款详情失败", arg0
											.getLocalizedMessage());
								}
							});
				} // end if for refund

				itemInfoVP.clear();
				itemInfoVP.add(ft);
			} // end if for tempB.getText()
		}
	}

	/**
	 * @return The refund apply page
	 */
	public Composite getRefundApplyPage() {
		return refundApplyPage;
	}

	/**
	 * @return The update refund term page
	 */
	public Composite getUpdateRefundPage() {
		return rTUPage;
	}

	/**
	 * @return The good confirmation page
	 */
	public Composite getGoodConfirmPage() {
		return gCPage;
	}

	/**
	 * @return The page of the buyer's returning goods to the seller
	 */
	public Composite getBuyerReturnGoods() {
		return bRGPage;
	}

	/**
	 * Initialize the flex table
	 */
	private void initTable() {
		// Create the header row.
		flexTable.setText(0, 0, "图片");
		flexTable.setText(0, 1, "标题");
		flexTable.setText(0, 2, "价格");
		flexTable.setText(0, 3, "交易ID");
		flexTable.setText(0, 4, "状态");
		flexTable.setText(0, 5, "卖家");
		flexTable.setText(0, 6, "查看详情");

		flexTable.setWidget(0, 7, navBar);

		// Initialize the rest of the rows.
		for (int i = 0; i < VISIBLE_ITEM_COUNT; ++i) {
			flexTable.setText(i + 1, 0, "");
			flexTable.setText(i + 1, 1, "");
			flexTable.setText(i + 1, 2, "");
			flexTable.setText(i + 1, 3, "");
			flexTable.setText(i + 1, 4, "");
			flexTable.setText(i + 1, 5, "");
			flexTable.setText(i + 1, 6, "");
		}
	}

	// public void search(String tid){
	// //1.get TopTrade
	// TopTrade t = new TopTrade();
	// bil = new ArrayList();
	// bil.add(t);
	// update();
	// }
	//	
	// public void showTotal(){
	// bil = total;
	// update();
	// }

	public void update() {
		// Update the older/newer buttons & label.
		if (bil == null)
			return;
		int count = bil.size();
		int max = startIndex + VISIBLE_ITEM_COUNT;
		if (max > count) {
			max = count;
		}

		newerButton.setVisible(startIndex != 0);
		olderButton.setVisible(startIndex + VISIBLE_ITEM_COUNT < count);
		countLabel
				.setText("" + (startIndex + 1) + " - " + max + " of " + count);

		// Show the selected trades.
		int i = 0;
		for (; i < VISIBLE_ITEM_COUNT; ++i) {
			// Don't read past the end.
			if (startIndex + i >= count) {
				break;
			}
			// A vertical panel to store the actions a user can do
			VerticalPanel actions = new VerticalPanel();

			final TopTrade trade = bil.get(startIndex + i);

			String id = trade.getTid();

			boolean hasOrders = false;
			// 如果有多个子订单,那么显示不一样
			if (trade.getTopOrders().size() > 1) {
				hasOrders = true;
				Button bt = new Button("查看子订单");
				bt.addClickHandler(new ClickHandler() {
					@Override
					public void onClick(ClickEvent arg0) {
						showTopOrder(trade);
					}
				});
				actions.add(bt);
			}
			;
			//

			// Get the refund according to the trade id
			TopRefund refund = getRefund2(id);
			// The corresponding trade is refunded
			if (refund != null) {
				// Show the return goods button
				if (refund.getStatus().equalsIgnoreCase(
						"WAIT_BUYER_RETURN_GOODS")) {
					Button retGooB = new CustomButton("退货");
					retGooB.addClickHandler(this);

					// Set the trade id and the refund id
					((CustomButton) retGooB).setTrade(trade);
					((CustomButton) retGooB).setTradeId(id);
					((CustomButton) retGooB).setRefundId(refund.getRefundId());

					// Add to the alternative actions
					actions.add(retGooB);
				}
			}
			// Add a new row to the table, then set each of its columns to the
			// trade's picture, title, price, trade id and status.
			Image image = new Image(trade.getPicPath());
			image.setSize("30px", "30px");
			flexTable.setWidget(i + 1, 0, image);

			flexTable.setText(i + 1, 1, trade.getTopOrders().get(0).getTitle());
			flexTable.setText(i + 1, 2, trade.getPrice());
			flexTable.setText(i + 1, 3, trade.getTid());
			flexTable.setText(i + 1, 4, StatusMap.get(trade.getStatus()));
			flexTable.setText(i + 1, 5, trade.getSellerNick());

			Button viewB = new CustomButton("查看详情");
			((CustomButton) viewB).setTrade(trade);
			viewB.addClickHandler(this);
			flexTable.setWidget(i + 1, 6, viewB);

			flexTable.setWidget(i + 1, 7, actions);

			if (trade.getStatus().equalsIgnoreCase("WAIT_SELLER_SEND_GOODS")
					|| trade.getStatus().equalsIgnoreCase(
							"WAIT_BUYER_CONFIRM_GOODS")) {

				if (!hasOrders) {
					Button refundButton = new CustomButton("申请退款");
					refundButton.addClickHandler(this);

					((CustomButton) refundButton).setTrade(trade);
					((CustomButton) refundButton).setTradeId(id);

					actions.add(refundButton);
				}
			} else if (trade.getStatus()
					.equalsIgnoreCase("TRADE_NO_CREATE_PAY")
					|| trade.getStatus().equalsIgnoreCase("WAIT_BUYER_PAY")) {
				// Add the pay button
				Button payButton = new CustomButton("付款");
				payButton.addClickHandler(this);

				// Set the trade id and the trade.
				((CustomButton) payButton).setTrade(trade);
				((CustomButton) payButton).setTradeId(id);
				actions.add(payButton);
			}

			if (trade.getStatus().equalsIgnoreCase("WAIT_BUYER_CONFIRM_GOODS")) {
				// Add a button of confirming the receipt of the goods
				Button confirmButton = new CustomButton(confirmProduct);
				confirmButton.addClickHandler(this);
				// Set the trade id and the trade.
				((CustomButton) confirmButton).setTrade(trade);
				((CustomButton) confirmButton).setTradeId(id);
				actions.add(confirmButton);
			}

			// add memo
			Button addTradeMemoButton = new CustomButton(addTradeMemo);
			addTradeMemoButton.addClickHandler(this);

			((CustomButton) addTradeMemoButton).setTrade(trade);
			((CustomButton) addTradeMemoButton).setTradeId(trade.getTid());

			actions.add(addTradeMemoButton);
			// add memo end
		}

		// Clear any remaining slots.
		for (; i < VISIBLE_ITEM_COUNT; ++i) {
			flexTable.setHTML(i + 1, 0, "&nbsp;");
			flexTable.setHTML(i + 1, 1, "&nbsp;");
			flexTable.setHTML(i + 1, 2, "&nbsp;");
			flexTable.setHTML(i + 1, 3, "&nbsp;");
			flexTable.setHTML(i + 1, 4, "&nbsp;");
			flexTable.setHTML(i + 1, 5, "&nbsp;");
			flexTable.setHTML(i + 1, 6, "&nbsp;");
			flexTable.setHTML(i + 1, 7, "&nbsp;");
		}
	}

	final String confirmProduct = "确认";

	protected void showTopOrder(TopTrade t) {
		final DialogBox dialogBox = new DialogBox();
		dialogBox.setText("子订单查看");
		dialogBox.setAnimationEnabled(true);

		Button closeButton = new Button("Close");
		// We can set the id of a widget by accessing its
		// Element
		closeButton.getElement().setId("closeButton");

		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogBox.setWidget(dialogVPanel);

		List<TopOrder> topOrders = t.getTopOrders();

		int i = 0;

		final FlexTable flexTable = new FlexTable();

		final SimplePanel orderDetailPanel = new SimplePanel();

		flexTable.setWidth("800px");
		flexTable.setBorderWidth(2);
		flexTable.setCellPadding(5);
		flexTable.setCellSpacing(5);
		
		for (TopOrder o : topOrders) {
			// A vertical panel to store the actions a user can do
			VerticalPanel actions = new VerticalPanel();

			// Get the refund according to the trade id
			TopRefund refund = getRefund2(o.getTid());
			// The corresponding trade is refunded
			if (refund != null) {
				// Show the update refund term button
				// if
				// (o.getStatus().equalsIgnoreCase("WAIT_BUYER_CONFIRM_GOODS")
				// &&
				// (
				// (!refund.getGoodStatus().equalsIgnoreCase("BUYER_RETURNED_GOODS")
				// &&
				// (
				// refund.getStatus().equalsIgnoreCase("WAIT_SELLER_AGREE") ||
				// refund.getStatus().equalsIgnoreCase("SELLER_REFUSE_BUYER")
				// ))
				// ||
				// (refund.getGoodStatus().equalsIgnoreCase("BUYER_RETURNED_GOODS")
				// && (
				// refund.getStatus().equalsIgnoreCase("SELLER_REFUSE_BUYER")))
				//								
				// )) {
				Button updateRefTerB = new CustomButton("修改退款协议");
				updateRefTerB.addClickHandler(this);

				// Set the trade, the trade id and the refund id
				((CustomButton) updateRefTerB).setOrder(o);
				((CustomButton) updateRefTerB).setTradeId(o.getTid());
				((CustomButton) updateRefTerB)
						.setRefundId(refund.getRefundId());
				// Add to the alternative actions
				actions.add(updateRefTerB);
				// }

				// Show the return goods button
				if (refund.getStatus().equalsIgnoreCase(
						"WAIT_BUYER_RETURN_GOODS")) {
					Button retGooB = new CustomButton("退货");
					retGooB.addClickHandler(this);

					// Set the trade id and the refund id
					// ((CustomButton) retGooB).setTrade(trade);
					((CustomButton) retGooB).setOrder(o);
					((CustomButton) retGooB).setTradeId(o.getTid());
					((CustomButton) retGooB).setRefundId(refund.getRefundId());

					// Add to the alternative actions
					actions.add(retGooB);
				}
			}
			// Add a new row to the table, then set each of its columns to the
			// trade's picture, title, price, trade id and status.
			Image image = new Image(o.getPicPath());
			image.setSize("30px", "30px");
			flexTable.setWidget(i + 1, 0, image);

			flexTable.setText(i + 1, 1, o.getTitle());
			flexTable.setText(i + 1, 2, o.getPrice());
			flexTable.setText(i + 1, 3, o.getTid());
			flexTable.setText(i + 1, 4, StatusMap.get(o.getStatus()));
			flexTable.setText(i + 1, 5, o.getSellerNick());
			flexTable.setWidget(i + 1, 6, actions);

			if (o.getStatus().equalsIgnoreCase("WAIT_SELLER_SEND_GOODS")
					|| o.getStatus().equalsIgnoreCase(
							"WAIT_BUYER_CONFIRM_GOODS")) {
				Button refundButton = new CustomButton("申请退款");
				refundButton.addClickHandler(this);

				((CustomButton) refundButton).setOrder(o);
				((CustomButton) refundButton).setTradeId(o.getTid());
				((CustomButton) refundButton).setTrade(t);

				actions.add(refundButton);
			}
			// if (o.getStatus().equalsIgnoreCase("TRADE_NO_CREATE_PAY")
			// || o.getStatus().equalsIgnoreCase("WAIT_BUYER_PAY")) {
			// // Add the pay button
			// Button payButton = new CustomButton("付款");
			// payButton.addClickHandler(this);
			//
			// // Set the trade id and the trade.
			// ((CustomButton) payButton).setOrder(o);
			// ((CustomButton) payButton).setTradeId(o.getTid());
			// actions.add(payButton);
			// }

			if (o.getStatus().equalsIgnoreCase("WAIT_BUYER_CONFIRM_GOODS")) {
				// Add a button of confirming the receipt of the goods
				Button confirmButton = new CustomButton(confirmProduct);
				confirmButton.addClickHandler(this);
				// Set the trade id and the trade.
				((CustomButton) confirmButton).setOrder(o);
				((CustomButton) confirmButton).setTradeId(o.getTid());
				actions.add(confirmButton);
			}/**/

			//查看详情按钮
			CustomButton showDetail = new CustomButton("详情");
			showDetail.setOrder(o);
			actions.add(showDetail);
			showDetail.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent arg0) {
					CustomButton source = (CustomButton) arg0.getSource();
					showOrderDetail(orderDetailPanel, source.getOrder());
				}
			});
			i++;
		}

		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
			}
		});

		dialogVPanel.add(flexTable);
		dialogVPanel.add(closeButton);
		dialogBox.show();
		dialogBox.center();

		dialogVPanel.add(orderDetailPanel);

		// flexTable.addClickHandler(new ClickHandler() {
		//			
		// @Override
		// public void onClick(ClickEvent arg0) {
		// Cell cell = flexTable.getCellForEvent(arg0);
		// if (cell != null) {
		//	
		// }
		// }
		// });
	}

	/**
	 * 显示子订单列表中的订单详情
	 * @param orderDetailPanel
	 * @param topOrder
	 */
	private void showOrderDetail(SimplePanel orderDetailPanel, TopOrder topOrder) {
		// Display the concrete information about one item.
		TopRefund refund = getRefund2(topOrder.getTid());

		TopOrder tt = topOrder;

		int i = 0;
		FlexTable ft = new FlexTable();
		ft.setText(i, 0, "商品标题");
		ft.setText(i, 1, tt.getTitle());
		ft.setText(++i, 0, "价格");
		ft.setText(i, 1, tt.getPrice());
		ft.setText(++i, 0, "交易状态");
		ft.setText(i, 1, StatusMap.get(tt.getStatus()));

		// ft.setText(++i, 0, "收货地址");
		// ft.setText(i, 1, tt.getReceiverAddress());
		//			
		// ft.setText(++i, 0, "收货人");
		// ft.setText(i, 1, tt.getReceiverName());

		ft.setBorderWidth(2);
		if (refund != null) {
			ft.setText(++i, 0, "退款信息");

			ft.getFlexCellFormatter().setColSpan(i, 0, 2);
			ft.getFlexCellFormatter().setHorizontalAlignment(i, 0,
					HorizontalPanel.ALIGN_LEFT);

			ft.setText(++i, 0, "退款状态");
			ft.setText(i, 1, StatusMap.get(refund.getStatus()));

			ft.setText(++i, 0, "退款人");
			ft.setText(i, 1, refund.getBuyerNick());
			ft.setText(++i, 0, "退款地址");
			ft.setText(i, 1, refund.getAddress());
			// good_return_time
			ft.setText(++i, 0, "退货时间");
			ft.setText(i, 1, (refund.getGoodReturnTime()) == null ? "null"
					: refund.getGoodReturnTime().toString());

			ft.setText(++i, 0, "货物状态");
			ft.setText(i, 1, StatusMap.get(refund.getGoodStatus()));

			// ft.setText(++i, 0, "交易状态");
			// ft.setText(i, 1, refund.getOrderStatus());
		}
		orderDetailPanel.clear();
		orderDetailPanel.add(ft);
	}

	/**
	 * 
	 * @param tradeId
	 *            the trade id
	 * @return if the trade is refunded, return the refund object, or return
	 *         null
	 */
	private TopRefund getRefund2(String tradeId) {
		// Have a list of refunds
		if (trList != null) {
			for (TopRefund tRefund : trList) {
				// The trade id in tRefund is equal to tradeId
				if (tRefund.getOid().equalsIgnoreCase(tradeId)) {
					return tRefund;
				}
			}
			for (TopRefund tRefund : trList) {
				// The trade id in tRefund is equal to tradeId
				if (tRefund.getTid().equalsIgnoreCase(tradeId)) {
					return tRefund;
				}
			}

		}
		return null;
	}
}
